<?php
/**
 * Created by PhpStorm.
 * User: gaochaolyf
 * Date: 16/5/19
 * Time: 下午2:11
 */

namespace api\Model;

use Think\Model;

class CustEntityModel extends Model
{
    function __construct() {
        $this->model = M('CustEntity');
        $this->model1 = M('CustFans');
        $this->model2 = M('CommentEntity');
        $this->model3 = M('DiaryResponse');
        $this->model4 = M('Report');
        $this->model5 = M('DiaryEntity');
        $this->model6=M('DoctorEntity');
        $this->model7=M('HosptEntity');
        $this->model8=M('IntegerEntity');
    }

    public function getCustList($field = array('*'), $condition = array(), $page = 1, $pergage = 10, $order = 'cust_id ASC') {
        $data = $this->model->where($condition)->field($field)->page($page, $pergage)->order($order)->select();
        $data = $data ? $data : array();
        return $data;
    }

    public function getOneCust($field = array('*'), $condition = array()) {
        //print_r($condition);die;
        $data = $this->model->where($condition)->field($field)->find();
        $data = $data ? $data : array();
        return $data;
    }


    public function addOneCust($param = array()) {
        $insert_id = $this->model->data($param)->add();
        return $insert_id;
    }

    public function orreport($param = array()) {
        $data = $this->model4->where($param)->find();
        return $data;
    }

    public function report($param = array()) {
        $insert_id = $this->model4->data($param)->add();
        if($insert_id){
           // echo $param['type'];die;
            switch ($param['type']){
                case 1:
                    $data1 = $this->model5->where("diary_id='".$param['diary_id']."'")->getField('report_num');
                    $newreport_num=$data1+1;
                    $this->model5->where("diary_id='".$param['diary_id']."'")->save(array('report_num'=>$newreport_num));
                    break;
                case 2:
                    $data1 = $this->model->where("cust_id='".$param['cust_id']."'")->getField('report_num');
                    $newreport_num=$data1+1;
                    $this->model->where("cust_id='".$param['cust_id']."'")->save(array('report_num'=>$newreport_num));
                    break;
                case 3:
                    $data1 = $this->model3->where("id='".$param['response_id']."'")->getField('report_num');
                    $newreport_num=$data1+1;
                    $this->model3->where("id='".$param['response_id']."'")->save(array('report_num'=>$newreport_num));
                    break;
            }
        }
        return $insert_id;
    }

    public function updateOneCust($param = array(), $condition = array()) {
        $affected = $this->model->where($condition)->setField($param);
        return $affected;
    }

    public function saveOneCust($param = array()) {
        $insert_id = $this->model->data($param)->add();
        return $insert_id;
    }
//修改个人资料
    public function saveCustinfo($param = array(), $condition = array()) {
        $affected = $this->model->where($condition)->save($param);
        return $affected;
    }
    
    //我的粉丝
    public function myfans($type='0',$cust_id='') {
        $data = $this->model1->where("cust_id='".$cust_id."'")->order('add_at desc')->select();
        foreach($data as $key=>$val) {
            $data1[] = $this->model->where("cust_id='".$val['fans_id']."'")->field('cust_id,nickname,autograph,cust_img')->find();
        }
        return $data1;
    }

    //我的关注
    public function myguan($type='0',$fans_id='') {
        $data = $this->model1->where("fans_id='".$fans_id."' and type='".$type."'")->select();
        foreach($data as $key=>$val) {
            $data1[] = $this->model->where("cust_id='".$val['cust_id']."'")->field('cust_id,nickname,autograph,cust_img')->find();
        }
        return $data1;
    }


    //我的关注
    public function myguan_all($fans_id='') {
        $data = $this->model1->where("fans_id='".$fans_id."'")->select();
        foreach($data as $key=>$val) {
            $data1[] = $this->model->where("cust_id='".$val['cust_id']."'")->field('cust_id,nickname,autograph,cust_img')->find();
        }
        return $data1;
    }



    //我的关注
    public function myguan1($fans_id='') {
        $data = $this->model1->where("fans_id='".$fans_id."'")->order('add_at desc')->select();
         $i=0;$m=0;$k=0;$n=0;
        foreach ($data as $key=>$val){
            if($val['type']==0){
                $data1['user'][$i] = $this->model->where("cust_id='".$val['cust_id']."'")->field('cust_id,nickname as name,autograph as graph,cust_img as cust_img')->find();
                if(empty($data1['user'][$i])){
                    $this->model1->where(array('id'=>$val['id']))->delete();
                }
                else{
                    $data1['user'][$i]['type']=0;
                }
                $i++;
            }
            if($val['type']==1){
                $data1['doctor'][$m] = $this->model6->where("doctor_id='".$val['cust_id']."'")->field('doctor_id as cust_id,doctor_name as name,doctor_desc as graph,doctor_pic as cust_img')->find();
                if(empty($data1['doctor'][$m])){
                    $this->model1->where(array('id'=>$val['id']))->delete();
                }
                else{
                    $data1['doctor'][$m]['type']=1;
                }
                $m++;
            }
            if($val['type']==2){
                $data1['hospt'][$k] = $this->model7->where("hospt_id='".$val['cust_id']."'")->field('hospt_id as cust_id, hospt_title as name,hospt_desc as graph,hospt_pic as cust_img')->find();
                if(empty($data1['hospt'][$k])){
                    $this->model1->where(array('id'=>$val['id']))->delete();
                }
                else{
                    $data1['hospt'][$k]['type']=2;
                }
                $k++;
            }
            if($val['type']==3){
                $data1['integer'][$n] = $this->model8->where("integer_id='".$val['cust_id']."'")->field('integer_id as cust_id,integer_name as name,integer_intro as graph,integer_pic as cust_img')->find();
                if(empty($data1['integer'][$n])){
                    $this->model1->where(array('id'=>$val['id']))->delete();
                }
                else{
                    $data1['integer'][$n]['type']=3;
                }
                $n++;
            }
        }
        return $data1;
    }

    //关注成为粉丝
    public function Befans($param = array()) {
        $insert_id = $this->model1->data($param)->add();
        return $insert_id;
    }

    //删除关注
    public function deletefans($type=0,$fans_id='',$cust_id='') {
        $dele = $this->model1->where("cust_id='".$cust_id."' and fans_id='".$fans_id."' and type='".$type."'")->delete();
        return $dele;
    }

    //是否是粉丝
    public function orfans($type=0,$fans_id='',$cust_id='') {
        $data = $this->model1->where("cust_id='".$cust_id."' and fans_id='".$fans_id."' and type='".$type."'")->getField('id');
       // echo $this->model1->_sql();die;
        $data= $data ? $data : array();
        return $data;
    }


    //评论--日记
    public function MyComment($cust_id='') {
        $data1 = $this->model3->where("response_cust_id='".$cust_id."'")->order('add_at desc')->select();
        foreach ($data1 as $key=>$val){
            $diary_info = $this->model5->where("diary_id='".$val['diary_id']."'")->find();
            $cust_info = $this->model->where("cust_id='".$diary_info['cust_id']."'")->find();
            $data1[$key]['diaryInfo']['diary_diary_id']=$diary_info['diary_id'];
            $data1[$key]['diaryInfo']['diary_cust_id']=$diary_info['cust_id'];
            $data1[$key]['diaryInfo']['diary_add_at']=$diary_info['add_at'];
            $data1[$key]['diaryInfo']['diary_cust_nickname']=$cust_info['nickname'];
            $data1[$key]['diaryInfo']['diary_cust_img']=$cust_info['cust_img'];
            $data1[$key]['diaryInfo']['diary_content']=$diary_info['diary_content'];
        }
        $data1= $data1 ? $data1 : array();
        return $data1;
    }

    //删除评论
    public function deletecomment($param = array()) {
        $info=$this->model3->where($param)->find();

        //print_r($info);die;

        $del_info=$this->model3->where(array('id'=>$param['id']))->delete();
        
        $this->get_child($info['diary_id'],$info['id']);


        return $del_info;
    }



    public function get_child($diary_id,$id){
        $arr = array();
        $rows = $this->model3->where("diary_id ='".$diary_id."' and response_id = '".$id."'")->field('id')->select();
        if($rows){
            foreach($rows as $k=>$v){
                $this->model3->where(array('id'=>$v['id']))->delete();
                $rows[$k]['list'] = $this->get_child($diary_id,$v['id']);
                $arr[] = $rows;//组合数组
            }
            return $rows;
        }
    }


    public function diaryComment($did)
    {
        $info = $this->model1->where("diary_id='" . $did . "' and response_id=0")->select();
        foreach($info as $key=>$val){
            $ins=$this->get_child($val['diary_id'],$val['response_id']);
        }
        return $ins;
    }

}